package xcmg.device.web.purchase;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.dao.entity.purchaseInvoice.ReqApplyExamine;
import xcmg.device.dao.entity.purchaseInvoice.ReqPurchaseInvoiceApply;
import xcmg.device.dao.entity.purchaseInvoice.ReqPurchaseInvoiceCreate;
import xcmg.device.dao.entity.purchaseInvoice.ReqPurchaseInvoiceExportParam;
import xcmg.device.dao.entity.purchaseInvoice.ReqPurchaseInvoiceList;
import xcmg.device.dao.entity.purchaseInvoice.ReqPurchaseInvoiceUpdate;
import xcmg.device.dao.entity.purchaseInvoice.ResInvoiceApply;
import xcmg.device.dao.entity.purchaseInvoice.ResInvoiceApplyDetail;
import xcmg.device.dao.entity.purchaseInvoice.ResPurchaseInvoiceList;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.purchase.PurchaseInvoiceApplyService;
import yb.ecp.fast.infra.infra.ActionResult;

import java.util.Arrays;
import java.util.Map;


@RestController
@RequestMapping( "/purchaseInvoiceApply" )
public class PurchaseInvoiceApplyController extends BasicController
{
    private Logger logger = LoggerFactory.getLogger( getClass() );

    @Autowired
    private PurchaseInvoiceApplyService purchaseInvoiceApplyService;

    /**
     * list
     * @description <获取采购开票列表>
     * @param reqPurchaseInvoiceList
     * @param pageNum
     * @param pageSize
     * @return
     * @return ActionResult 返回类型
     * @author guoxin
     * @version V1.0
     * @date 2019年9月10日
     * @email guoxin@redoornetwork.com
     */
    @SuppressWarnings( "rawtypes" )
    @ApiOperation( value = "采购开票列表查询 " )
    @ApiImplicitParams( { @ApiImplicitParam( name = "pageNum", value = "页码", paramType = "query", dataType = "int", required = true ),
            @ApiImplicitParam( name = "pageSize", value = "每页条数", paramType = "query", dataType = "int", required = true ) } )
    @PostMapping( "/list" )
    public ActionResult list(@RequestBody ReqPurchaseInvoiceList reqPurchaseInvoiceList, Integer pageNum, Integer pageSize ) {
        try {
            logger.info( "查询采购开票列表开始,reqPurchaseInvoiceList={},pageNum={},pageSize={}", reqPurchaseInvoiceList, pageNum, pageSize );
            PageInfo<ResPurchaseInvoiceList> page = purchaseInvoiceApplyService.getList( reqPurchaseInvoiceList, getOrgId(), pageNum, pageSize );
            logger.info( "查询采购开票列表结束,page={}", page );
            return actionResult( ErrorCode.Success, page );
        } catch ( Exception e ) {
            logger.error( "查询采购开票列表异常", e );
            e.printStackTrace();
            return actionResult( ErrorCode.Failure );
        }
    }

    /**
     * applyList
     * @description <获取开票申请列表>
     * @param reqPurchaseInvoiceApply
     * @param pageNum
     * @param pageSize
     * @return
     * @return ActionResult 返回类型
     * @author guoxin
     * @version V1.0
     * @date 2019年9月24日
     * @email guoxin@redoornetwork.com
     */
    @SuppressWarnings( "rawtypes" )
    @ApiOperation( value = "开票申请列表查询 " )
    @ApiImplicitParams( { @ApiImplicitParam( name = "pageNum", value = "页码", paramType = "query", dataType = "int", required = true ),
            @ApiImplicitParam( name = "pageSize", value = "每页条数", paramType = "query", dataType = "int", required = true ) } )
    @PostMapping( "/applyList" )
    public ActionResult applyList(@RequestBody ReqPurchaseInvoiceApply reqPurchaseInvoiceApply, Integer pageNum, Integer pageSize ) {
        try {
            logger.info( "查询开票申请列表开始,reqPurchaseInvoiceApply={},pageNum={},pageSize={}", reqPurchaseInvoiceApply, pageNum, pageSize );
            PageInfo<ResInvoiceApply> page = purchaseInvoiceApplyService.getApplyList( reqPurchaseInvoiceApply, getOrgId(), pageNum, pageSize );
            logger.info( "查询开票申请列表结束,page={}", page );
            return actionResult( ErrorCode.Success, page );
        } catch ( Exception e ) {
            logger.error( "查询开票申请列表异常", e );
            e.printStackTrace();
            return actionResult( ErrorCode.Failure );
        }
    }


    @ApiOperation( value = "开票申请是否暂估价查询 " )
    @PostMapping( "/queryProvisional" )
    public ActionResult queryProvisional(@RequestBody ReqPurchaseInvoiceCreate reqPurchaseInvoiceCreate) {
        try {
            Map<String, Object> result = purchaseInvoiceApplyService.queryProvisional(reqPurchaseInvoiceCreate.getResInvoiceApplyList(), getOrgId());
            ErrorCode errorCode = (ErrorCode) result.get("errorCode");
            Map<String, String> value = null;
            if (result.get("value") != null) {
                value = (Map<String, String>) result.get("value");

            }
            return actionResult(errorCode,value);
        } catch ( Exception e ) {
            logger.error( "查询开票申请列表异常", e );
            e.printStackTrace();
            return actionResult( ErrorCode.Failure );
        }
    }

    /**
     * addApply
     * @description <新增采购开票申请>
     * @param reqPurchaseInvoiceCreate
     * @return
     * @return ActionResult 返回类型
     * @author guoxin
     * @version V1.0
     * @date 2019年9月27日
     * @email guoxin@redoornetwork.com
     */
    @ApiOperation( value = "新增采购开票申请 " )
    @PostMapping( "/addApply" )
    @SuppressWarnings( "rawtypes" )
    public ActionResult addApply( @RequestBody ReqPurchaseInvoiceCreate reqPurchaseInvoiceCreate ) {
        try {
            logger.info( "新增采购开票申请开始" );
            ErrorCode code = purchaseInvoiceApplyService.addPurchaseInvoiceApply( reqPurchaseInvoiceCreate, getOrgId(), getUserId(), getUserName() );
            logger.info( "新增采购开票申请完成" );
            return actionResult( code );
        } catch ( Exception e ) {
            logger.error( "新增采购开票申请异常", e );
            e.printStackTrace();
            return actionResult( ErrorCode.Failure );
        }
    }

    /**
     * updateApply
     * @description <更新采购开票申请>
     * @param reqPurchaseInvoiceUpdate
     * @return
     * @return ActionResult 返回类型
     * @author guoxin
     * @version V1.0
     * @date 2019年9月30日
     * @email guoxin@redoornetwork.com
     */
    @ApiOperation( value = "更新采购开票申请 " )
    @PostMapping( "/updateApply" )
    @SuppressWarnings( "rawtypes" )
    public ActionResult updateApply( @RequestBody ReqPurchaseInvoiceUpdate reqPurchaseInvoiceUpdate ) {
        try {
            logger.info( "更新采购开票申请开始" );
            ErrorCode code = purchaseInvoiceApplyService.updatePurchaseInvoiceApply( reqPurchaseInvoiceUpdate );
            logger.info( "更新采购开票申请完成" );
            return actionResult( code );
        } catch ( Exception e ) {
            logger.error( "更新采购开票申请异常", e );
            e.printStackTrace();
            return actionResult( ErrorCode.Failure );
        }
    }

    /**
     * materialsInApply
     * @description <获取某个采购开票申请单中的零件列表>
     * @param invoiceApplyId
     * @return
     * @return ActionResult 返回类型
     * @author guoxin
     * @version V1.0
     * @date 2019年10月2日
     * @email guoxin@redoornetwork.com
     */
    //	@SuppressWarnings( "rawtypes" )
    //	@ApiOperation( value = "获取某个采购开票申请单中的零件列表" )
    //	@ApiImplicitParams( {
    //	        @ApiImplicitParam( name = "invoiceApplyId", value = "采购开票申请单id", paramType = "query", dataType = "string", required = true ) } )
    //	@GetMapping( "/materialsInApply" )
    //	public ActionResult materialsInApply( String invoiceApplyId, Integer pageNum, Integer pageSize )
    //	{
    //		try
    //		{
    //			logger.info( "获取id={}的采购开票申请单中的零件列表开始", invoiceApplyId );
    //			PageInfo<ResMaterialsInApply> page = purchaseInvoiceApplyService.materialsInApply( invoiceApplyId, pageNum, pageSize );
    //			logger.info( "查询id={}的采购开票申请单中的零件列表结束,page={}", invoiceApplyId, page );
    //			return actionResult( ErrorCode.Success, page );
    //		}
    //		catch ( Exception e )
    //		{
    //			logger.error( "查询id={}的采购开票申请单中的零件列表异常", e );
    //			e.printStackTrace();
    //			return actionResult( ErrorCode.Failure );
    //		}
    //	}

    /**
     * examine
     * @description <采购开票申请审核 >
     * @param reqApplyExamine
     * @return
     * @return ActionResult 返回类型
     * @author guoxin
     * @version V1.0
     * @date 2019年10月2日
     * @email guoxin@redoornetwork.com
     */
    @ApiOperation( value = "采购开票申请审核 " )
    @PostMapping( "/examine" )
    @SuppressWarnings( "rawtypes" )
    public ActionResult examine( @RequestBody ReqApplyExamine reqApplyExamine ) {
        try {
            logger.info( "更新采购开票申请审核开始" );
            ErrorCode code = purchaseInvoiceApplyService.examine( reqApplyExamine, getUserName() );
            logger.info( "更新采购开票申请审核完成" );
            return actionResult( code );
        } catch ( Exception e ) {
            logger.error( "更新采购开票申请审核异常", e );
            e.printStackTrace();
            return actionResult( ErrorCode.Failure );
        }
    }

    /**
     * deleteApply
     * @description <删除采购开票申请>
     * @param invoiceApplyId
     * @return
     * @return ActionResult 返回类型
     * @author guoxin
     * @version V1.0
     * @date 2019年10月2日
     * @email guoxin@redoornetwork.com
     */
    @ApiOperation( value = "删除采购开票申请 " )
    @PostMapping( "/deleteApply" )
    @SuppressWarnings( "rawtypes" )
    @ApiImplicitParams( {
            @ApiImplicitParam( name = "invoiceApplyId", value = "采购开票申请单id", paramType = "query", dataType = "string", required = true ) } )
    public ActionResult deleteApply( String invoiceApplyId ) {
        try {
            logger.info( "删除采购开票申请开始" );
            ErrorCode code = purchaseInvoiceApplyService.deletePurchaseInvoiceApply( invoiceApplyId );
            logger.info( "删除采购开票申请完成" );
            return actionResult( code );
        } catch ( Exception e ) {
            logger.error( "删除采购开票申请异常", e );
            e.printStackTrace();
            return actionResult( ErrorCode.Failure );
        }
    }

    /**
     * detail
     * @description <采购开票单详情>
     * @param invoiceApplyId
     * @return
     * @return ActionResult 返回类型
     * @author guoxin
     * @version V1.0
     * @date 2019年10月3日
     * @email guoxin@redoornetwork.com
     */
    @SuppressWarnings( "rawtypes" )
    @ApiOperation( value = "开票申请单详情 " )
    @ApiImplicitParams( {
            @ApiImplicitParam( name = "invoiceApplyId", value = "采购申请单id", paramType = "query", dataType = "string", required = true ) } )
    @GetMapping( "/detail" )
    public ActionResult detail( String invoiceApplyId ) {
        try {
            logger.info( "查询开票申请详情开始,invoiceApplyId={}", invoiceApplyId );
            ResInvoiceApplyDetail detail = purchaseInvoiceApplyService.detail( invoiceApplyId );
            if (detail == null) {
                logger.info( "未查询到有效信息,invoiceApplyId=" + invoiceApplyId );
                return actionResult( ErrorCode.InvoiceApplyNotExist );
            }
            logger.info( "查询开票申请详情结束,detail={}", detail );
            return actionResult( ErrorCode.Success, detail );
        } catch ( Exception e ) {
            logger.error( "查询开票申请列表异常", e );
            e.printStackTrace();
            return actionResult( ErrorCode.Failure );
        }
    }

    @PostMapping( value = "/export")
//	@FastMappingInfo( needLogin = true, code = AuthCode.SalesVerification.SALES_VERIFICATION_EXPORT )
    public void export( String[] ids ) throws Exception {
//		purchaseInvoiceApplyService.export( Arrays.asList( ids ),getOrgId() );
        purchaseInvoiceApplyService.exportDetail(Arrays.asList( ids ),getOrgId());
    }

    @RequestMapping( value = "/exportAll",method = RequestMethod.POST)
//	@FastMappingInfo( needLogin = true, code = AuthCode.SalesVerification.SALES_VERIFICATION_EXPORT_ALL )
    public void exportAll(ReqPurchaseInvoiceExportParam condition ) throws Exception {
//		purchaseInvoiceApplyService.exportAll( condition, getOrgId() );
        purchaseInvoiceApplyService.exportDetailAll(condition,getOrgId());
    }


}
